/**
 * Created by kevin on 2017/6/16.
 */
$('#package-btn').click(function () {
    var id = appId;
    if (!id || id.length === 0) {
        showError('请先选择应用')
        return;
    }
    $.post('/app/get',{id:id},function (data) {
        console.log(JSON.stringify(data));
        if (data.code === 0) {
            // 提示确认
            var app = data.data;
            $.confirm({
                title: '确定要打包' + app.name + "?",
                content: '打包过程您将无法停止打包行为',
                animationBounce: 1.5,
                animation: 'rotateXR',
                closeAnimation: 'rotateYR',
                buttons: {
                    ok: {
                        text: "确定",
                        btnClass: 'btn-success',
                        keys: ['enter'],
                        action: function(){
                            loadDelegaters(id, function (data) {
                                $.confirm({
                                    title: '请选择代理打包机',
                                    content: '需要选择代理机才可以进行',
                                    animationBounce: 1.5,
                                    animation: 'rotateXR',
                                    closeAnimation: 'rotateYR',
                                    buttons: data
                                });
                            })
                        }
                    },
                    cancel: {
                        text: "取消",
                        action:function(){

                        }
                    }
                }
            });
        } else {
            showError(data.message);
        }
    })
});

function archiveStart(appId,delId) {
    console.log('id ' + appId);
    $.post("/package/archive/android",{app_id:appId,del_id:delId}, function(data) {
        if (data.code === 0) {
            // 开始升级
            // 提示点击查看
            $.confirm({
                title: '已经开始打包进程',
                content: '是否要查看当前进度？',
                animationBounce: 1.5,
                animation: 'rotateXR',
                closeAnimation: 'rotateYR',
                buttons: {
                    ok: {
                        text: "去看看",
                        btnClass: 'btn-success',
                        keys: ['enter'],
                        action: function(){
                            loadPackageList();
                            // 跳转
                            archiveProgress(appId);
                        }
                    },
                    cancel: {
                        text: "先不了",
                        action:function(){
                            loadPackageList();
                        }
                    }
                }
            });

        } else  {
            showError(data.message);
        }
    });
}

function archiveProgressClick(node) {
    var id = $(node).attr('id');
    archiveProgress(id);
}

function archiveProgress(id) {
    window.open('/package/archive/' + id);

}

function loadDelegaters(id,callback) {
    $.post('/package/delegater/list',function (data) {
        if (data.code === 0) {
            var delegaters = data.data;
            if (delegaters && delegaters.length >0) {
                var list = {};
                delegaters.forEach(function (delegater,index) {
                    list[index] = {
                        text: delegater.name,
                        btnClass: 'btn-success',
                        keys: ['enter'],
                        action: function () {
                            archiveStart(id,delegater._id);
                        }
                    }
                })
                list.cancel = {
                    text: "取消",
                    action:function(){
                    }
                }
                callback(list);
            } else {
                var list = {
                    none : {
                        text: '暂无可用代理机',
                        btnClass: 'btn-success',
                        keys: ['enter'],
                        action: function () {

                        }
                    },
                    cancel: {
                        text: "取消",
                        action:function(){

                        }
                    }
                }
                callback(list)
            }
        } else {
            var list = {
                none: {
                    text: '暂无可用代理机',
                    btnClass: 'btn-success',
                    keys: ['enter'],
                    action: function () {

                    }
                },
                cancel: {
                    text: "取消",
                    action:function(){

                    }
                }
            }
            callback(list)
        }
    })
}